package com.example.project_4;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button btnBindService,btnUnbindService,btnGetStatus;
    TextView tvServiceStatus;
    MyService.MyServiceBinder serviceBinder;
    boolean isServiceBind=false;
    final static String TAG="Severtest";

    ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //返回 Service 的 Binder 对象
             serviceBinder =(MyService.MyServiceBinder)service;
             isServiceBind=true;
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnBindService=findViewById(R.id.btn_main_activity_bind_service);
        btnUnbindService=findViewById(R.id.btn_main_activity_unbind_service);
        btnGetStatus=findViewById(R.id.btn_main_activity_get_status);
        tvServiceStatus=findViewById(R.id.tv_main_activity_service_status);
        btnBindService.setOnClickListener(this);
        btnUnbindService.setOnClickListener(this);
        btnGetStatus.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        intent.setClass(MainActivity.this,MyService.class);
        switch (v.getId()){
            case R.id.btn_main_activity_bind_service:
                //如果 service 尚未绑定就绑定，如果已经绑定则忽略
               if(!isServiceBind) {
                   bindService(intent, conn, Context.BIND_AUTO_CREATE);
                   Toast.makeText(this, "Binding service", Toast.LENGTH_SHORT).show();
               }
               else
               {
                   Toast.makeText(this, "Services already bound", Toast.LENGTH_SHORT).show();
               }
                break;
            case R.id.btn_main_activity_get_status:
                if(isServiceBind) {
                    int t=serviceBinder.getCount();
                    tvServiceStatus.setText("： "+String.valueOf(t));
                    Toast.makeText(this, "To obtain state success", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(this,"Failed to get state，Not binding！",Toast.LENGTH_SHORT).show();
                }
//如果 service 已经绑定，获取 service 的 count 计数并显示在截面上。
                break;
            case R.id.btn_main_activity_unbind_service:

                if(isServiceBind) {
                    Toast.makeText(this,"The unbinding service succeeded. Procedure",Toast.LENGTH_SHORT).show();
                    unbindService(conn);
                    isServiceBind=false;
                }
                else
                {
                    Toast.makeText(this,"The unbinding service failed. Procedure，Not binding！",Toast.LENGTH_SHORT).show();
                }

//如果 service 已经绑定，则可以解绑，否则忽略
                break;
        }
    }
}
